from django.shortcuts import render

from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
import re,redis,json,time
from utils.ertf.wees import send_code
# Create your views here.



#登录
class Deng(APIView):
    def post(self,request):
        mobile=request.data.get('mobile')
        cs=request.data.get('cs')
        try:
            Lu.objects.get(mobile=mobile)
        except:
            return Response({'code':203,'msg':'手机号错误'})
        r=redis.Redis(host='localhost',port=6379,db=6)
        c1=r.get(mobile)
        if c1:
            c1=c1.decode()
        else:
            return Response({'code':204,'msg':'验证码过期'})
        if cs !=c1:
            return Response({'code':205,'msg':'验证码错误'})
        return Response({'code':200,'msg':'登录成功'})



#验证码
class Yan(APIView):
    def get(self,request):
        mobile = request.query_params.get('mobile')
        if not re.findall('^1[3-9]\d{9}$', mobile):
            return Response({'code': 205, 'msg': '手机号不正确'})
        resp = send_code.delay(mobile)
        time.sleep(5)
        try:
            cs = redis.Redis(host='127.0.0.1', port=6379, db=6)
            key = 'celery-task-meta-' + resp.id
            redis_data = cs.get(key)
            if redis_data:
                distct_resp = json.loads(redis_data.decode())
                if distct_resp['status'] == 'SUCCESS':
                    dict_data = json.loads(distct_resp['result'])
                    if dict_data['statusCode'] == '112310' or dict_data['statusCode'] == '000000':
                        return Response({'msg': '验证码发送成功', 'code': 200})
            else:
                return Response({'msg': '验证码发送失败', 'code': 206})
        except  Exception as  e:
            print('错误原因', e)
            return Response({'code': 500, 'msg': '后端错误'})



class FF(APIView):
    def get(self,request):
        set=Lu.objects.all()
        list=[]
        for i in set:
            list.append({
                'id':i.id,
                'mobile':i.mobile,
            })
        return Response(list)


#地址
class Zhi(APIView):
    def get(self,request):
        set=Di.objects.all()
        list=[]
        for i in set:
            list.append({
                'id':i.id,
                'name':i.name,
                'mobile':i.mobile,
                'mo':i.mo,
                'zhi':i.zhi
            })
        return Response(list)

    def post(self, request):
        name = request.data.get('name')
        mobile = request.data.get('mobile')
        mo= request.data.get('mo')
        zhi= request.data.get('zhi')
        Di.objects.create(
            name=name,
           mobile=mobile,
            mo=mo,
            zhi=zhi
        )
        return Response({'code': 200, 'msg': '添加成功'})


#修改和删除
class Gai(APIView):
    def delete(self,request,id):
        Di.objects.get(id=id).delete()
        return Response({'code': 200, 'msg': '删除成功'})
    def put(self,request,id):
        try:
            c1=Di.objects.get(id=id, mo=0)
            c1.mo=1
            c1.save()
            return Response({'code': 200, 'msg': '默认地址成功'})
        except Exception as e:
            print(e)
            user=Di.objects.get(id=id,mo=1)
            user.mo=0
            user.save()
            return Response({'code': 400, 'msg': "默认地址取消"})


#修改地址
class Xou(APIView):
    def get(self,request,id):
        set=Di.objects.filter(id=id)
        list=[]
        for i in set:
            list.append({
                'id':i.id,
                'zhi':i.zhi
            })
        return Response(list)
    def put(self,request,id):
        print('-----------', id)
        zhi = request.data.get('zhi')
        Di.objects.filter(id=id).update(zhi=zhi)
        return Response({'code': 200, 'msg': '修改成功'})
